export interface CallBack<K, V> {
    (value: V, key: K, de: Dictionaries<K, V>): void;
}
export class Dictionaries<K, V> {
    private _keys: K[] = [];
    private _values: V[] = [];

    private isEq(key1: K, key2: K): boolean {
        if (key1 === 0 && key2 === 0) {
            return true;
        }
        return Object.is(key1, key2);
    }
    private getIndex(key: K): number {
        return this._keys.findIndex((curKey) => this.isEq(curKey, key));
    }
    get size() {
        return this._keys.length;
    }
    has(key: K): boolean {
        return this.getIndex(key) !== -1;
    }
    set(key: K, value: V) {
        const result = this.getIndex(key);
        if (result !== -1) {
            this._values[result] = value;
        } else {
            this._keys.push(key);
            this._values.push(value);
        }
    }
    delete(key: K) {
        const index = this.getIndex(key);
        if (index !== -1) {
            this._keys.splice(index, 1);
            this._values.splice(index, 1);
            return true;
        }
        return false;
    }
    forEach(callBack: CallBack<K, V>, thisArg?: Object) {
        for (let index = 0; index < this.size; index++) {
            callBack.call(
                thisArg,
                this._values[index],
                this._keys[index],
                this
            );
        }
    }
}
